package com.example.seven.mynews.data;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.example.seven.mynews.activity.MainActivity;
import com.example.seven.mynews.activity.NewsActivity;
import com.example.seven.mynews.adapter.SimpleAdapter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by seven on 16-7-7.
 */
public class XMLPullToListData {

    private SimpleAdapter aSimpleAdapter;
    private  String url;


    public  void XMLPullDataInstance(View view,RecyclerView recyclerView,String url){
        this.url =url;
        xmlPullData(view,recyclerView);
    }

    /**
     * 开启网络下拉数据
     * @param view
     */
    public void xmlPullData(final View view,final RecyclerView recyclerView){

        RequestQueue requestQueue = Volley.newRequestQueue(view.getContext());
        XMLRequest xmlRequest = new XMLRequest(url,
                new Response.Listener<XmlPullParser>() {

                    @Override
                    public void onResponse(XmlPullParser xmlPullParser) {
                        getXmlToListData(xmlPullParser,view,recyclerView);
                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("TAG", volleyError.getMessage(), volleyError);
                    }
                }
        );


        requestQueue.add(xmlRequest);
    }

    /**
     *将XML数据解析成List
     * @param xmlPullParser
     * @param view
     */
    public void getXmlToListData(XmlPullParser xmlPullParser, View view,RecyclerView recyclerView) {
        List<News> newsList = null;

        try {
            News news = null;
            int i = 0;
            int j = 0;

            int evenType = xmlPullParser.getEventType();
            newsList = new ArrayList<>();
            //开始
            while (evenType != XmlPullParser.END_DOCUMENT) {
                //做循环，直到文档末端
                switch (evenType) {
                    //文档开始
                    case XmlPullParser.START_DOCUMENT:

                        break;
                    case XmlPullParser.START_TAG:
                        //Log.d("START_TAG","START_TAG is" + XmlPullParser.START_TAG);
                        //开始节点
                        String nodeName = xmlPullParser.getName();
                        //获取节点名称，进行选择
                        switch (nodeName) {
                            //每一个新闻
                            case "item":
                                news = new News();
                                break;
                            //新闻标题
                            case "title":
                                i++;
                                String title = xmlPullParser.nextText();
                                if (i>2) {
                                    //       Log.d("TAG", "title is " + title);
                                    news.setTitle(title);
                                }
                                break;
                            //新闻作者
                            case "author":
                                String author = xmlPullParser.nextText();
                                //    Log.d("TAG", "author is" + author);
                                news.setAuthor(author);
                                break;
                            //新闻发布时间
                            case "pubDate":
                                String pubData = xmlPullParser.nextText();
                                //  Log.d("TAG", "pubData is" + pubData);
                                news.setPubDate(pubData);
                                break;
                            //新闻内容
                            case "description":
                                j++;
                                String description = xmlPullParser.nextText();
                                if (j>1) {
                                    //  Log.d("TAG", "description is" + description);
                                    news.setDescription(description);
                                }
                                break;

                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String name = xmlPullParser.getName();
                        if ("item".equals(name)) {
                            newsList.add(news);
                            news = null;

                        }
                        break;
                }
                evenType = xmlPullParser.next();
            }


        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }



        initRecycler(view, newsList, recyclerView);

    }

    /**
     * 初始化recyclerView
     * @param view
     * @param data
     */
    public void initRecycler(final View view,List<News> data,RecyclerView recyclerView) {


        aSimpleAdapter = new SimpleAdapter(view.getContext(),data);

        recyclerView.setAdapter(aSimpleAdapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(view.getContext(),LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(layoutManager);

        aSimpleAdapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, Object object) {
                //Toast.makeText(view.getContext(), "你选中了第" + position + "条" + ((News) object).getTitle(), Toast.LENGTH_SHORT).show();
                showNewsDetail(view,((News) object));
            }
        });
    }

    public void showNewsDetail(View view,News news){
        Intent aIntent = new Intent(view.getContext(), NewsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("newsObject",news);//传递对象
        aIntent.putExtras(bundle);
        view.getContext().startActivity(aIntent);

    }

}
